﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using 为web添加unity.caching在controller中注入.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using 为web添加unity.caching在controller中注入.BLL;

namespace 为web添加unity.caching在controller中注入.Models
{
    public abstract class BaseController : Controller
    {

        static BaseController()
        {
            _container = new UnityContainer();
            //添加unity扩展,扩展类型是一个拦截器
            _container.AddNewExtension<Interception>();

            //为接口IOrderRepository注册拦截器,它的方式是接口拦截器,拦截器的实现是一个行为,它的实现体是Project.InterceptionBehaviors.CachingBehavior
            _container.RegisterType<IOrderRepository, OrderRepository>(
               new Interceptor<InterfaceInterceptor>(),
               new InterceptionBehavior<Project.UnityCaching.CachingBehavior>());

            _container.RegisterType<IUserRepository, UserRepository>(
                new Interceptor<InterfaceInterceptor>(),
                new InterceptionBehavior<Project.UnityCaching.CachingBehavior>());

            _container.RegisterType<IOrderService, OrderService>(
            new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<Project.UnityCaching.CachingBehavior>());

        }
        //创建容器  
        static readonly IUnityContainer _container;

        /// <summary>
        /// Gets the service instance with the given type.
        /// </summary>
        /// <typeparam name="T">The type of the service.</typeparam>
        /// <returns>The service instance.</returns>
        public T GetService<T>()
        {
            return _container.Resolve<T>();
        }
    }
}